package com.soccertgc.core.game
{
	import com.soccertgc.core.DataObject;
	
	public class GameMod extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------		
		
		// JSON data properties names
		public static const 	JSON_PROPERTY_ID:String					= "instanceId";
		public static const 	JSON_PROPERTY_NAME:String				= "name";
		public static const 	JSON_PROPERTY_DESCRIPTION:String		= "actionText";
		public static const 	JSON_PROPERTY_TYPE:String				= "subtype";
		
		// Default values
		private static const 	DEFAULT_ID:int							= 0;
		private static const 	DEFAULT_NAME:String						= "";
		private static const 	DEFAULT_DESCRIPTION:String				= "";
		private static const 	DEFAULT_TYPE:GameModType				= GameModType.GENERAL;
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _description:String;
		private var _type:GameModType;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameMod(data:Object = null)
		{
			// Clears all
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get type():GameModType
		{
			return _type;
		}
		
		public function set type(value:GameModType):void
		{
			_type = value;
		}

		public function get id():uint
		{
			return _id;
		}

		public function set id(value:uint):void
		{
			_id = value;
		}

		public function get name():String
		{
			return _name;
		}

		public function set name(value:String):void
		{
			_name = value;
		}

		public function get description():String
		{
			return _description;
		}

		public function set description(value:String):void
		{
			_description = value;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			if (!data.hasOwnProperty(JSON_PROPERTY_ID)) 			throwLoadMissingPropError(JSON_PROPERTY_ID);
			if (!data.hasOwnProperty(JSON_PROPERTY_NAME))			throwLoadMissingPropError(JSON_PROPERTY_NAME);
			if (!data.hasOwnProperty(JSON_PROPERTY_DESCRIPTION))	throwLoadMissingPropError(JSON_PROPERTY_DESCRIPTION);
			if (!data.hasOwnProperty(JSON_PROPERTY_TYPE))			throwLoadMissingPropError(JSON_PROPERTY_TYPE);
		}
		
		override protected function fill(data:Object):void
		{
			_id 			= uint(data[JSON_PROPERTY_ID]);
			_name			= data[JSON_PROPERTY_NAME];
			_description	= data[JSON_PROPERTY_DESCRIPTION];
			_type			= GameModType.getById(data[JSON_PROPERTY_TYPE]);
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function equals(mod:GameMod):Boolean
		{
			return	_id == mod.id;
		}
		
		public function copy(mod:GameMod):void
		{
			_id				= mod.id;
			_name			= mod.name;
			_description	= mod.description;
			_type			= mod.type;
		}
		
		public function clone():GameMod
		{
			var mod:GameMod = new GameMod();
			mod.copy(this);
			return mod;
		}
		
		override public function clear():void
		{
			// Clear data
			_id 			= DEFAULT_ID;
			_name			= DEFAULT_NAME;
			_description	= DEFAULT_DESCRIPTION;
			_type			= DEFAULT_TYPE;
		}
		

		public function toTreeObject(args:Object = null):Object
		{
			var treeObject:Object = 
				{	label: _name,
					children: 
					[
						{	label: "Id: " 			+ _id			},
						{	label: "Type: " 		+ _type.name	},
						{	label: "Description: " 	+ _description	}
					]
				};
			return treeObject; 
		}
		
	}
}